package com.xqboss.apps.service.synthesis;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.member.SysUserNft;
import com.xqboss.apps.domain.synthesis.SynthesisRecordConsumeResult;
import com.xqboss.apps.domain.synthesis.SynthesisUserRecord;
import com.xqboss.apps.dto.synthesis.SynthesisPlanConsumeDto;
import com.xqboss.apps.enums.synthesis.SynthesisRecordTypeEnum;
import com.xqboss.apps.mapper.synthesis.SynthesisRecordConsumeResultMapper;
import com.xqboss.apps.service.nft.NftNumberService;
import com.xqboss.apps.vo.synthesis.SynthesisStartResultVo;
import com.xqboss.chain.domain.chain.dto.BaseRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 合成记录消耗结果记录 服务实现类
 * </p>
 *
 * @author 木鱼
 * @since 2023-11-29
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SynthesisRecordConsumeResultService extends ServiceImpl<SynthesisRecordConsumeResultMapper, SynthesisRecordConsumeResult> {

    private final NftNumberService nftNumberService;

    public List<SynthesisRecordConsumeResult> selectBasicList(BaseRequest request) {
        return baseMapper.selectList(Wrappers.<SynthesisRecordConsumeResult>lambdaQuery());
    }

    public SynthesisRecordConsumeResult selectByIdBasic(Long id) {
        return baseMapper.selectById(id);
    }

    public boolean saveBasic(SynthesisRecordConsumeResult record) {
        return baseMapper.insert(record) > 0;
    }

    public boolean updateBasic(SynthesisRecordConsumeResult record) {
        return baseMapper.updateById(record) > 0;
    }

    public boolean removeBasic(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 存储合成记录
     * @param synthesisRecord
     * @param consumes
     * @param resultList
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRecord(SynthesisUserRecord synthesisRecord, List<SynthesisPlanConsumeDto> consumes, List<SynthesisStartResultVo> resultList) {
        List<SynthesisRecordConsumeResult> list = new ArrayList<>();
        for (SynthesisPlanConsumeDto consume : consumes) {
            switch (consume.getType()){
                case NORMAL:
                case EQUIPMENT:
                    for (SysUserNft userNft : consume.getBurnUserNfts()) {
                        SynthesisRecordConsumeResult record = new SynthesisRecordConsumeResult();
                        record.setRecordId(synthesisRecord.getId());
                        record.setRecordType(SynthesisRecordTypeEnum.CONSUME);
                        record.setName(consume.getName());
                        record.setImgUrl(consume.getImgUrl());
                        record.setType(consume.getType());
                        record.setThirdId(consume.getThirdId());
                        record.setOtherNo(nftNumberService.getNftNumber(userNft.getNftNumberId()).getNumberValueStr());
                        record.setNum(BigDecimal.ONE);
                        list.add(record);
                    }
                    break;
                default:
                    SynthesisRecordConsumeResult record = new SynthesisRecordConsumeResult();
                    record.setRecordId(synthesisRecord.getId());
                    record.setRecordType(SynthesisRecordTypeEnum.CONSUME);
                    record.setName(consume.getName());
                    record.setImgUrl(consume.getImgUrl());
                    record.setType(consume.getType());
                    record.setNum(BigDecimal.valueOf(consume.getNum()));
                    record.setThirdId(consume.getThirdId());
                    list.add(record);
                    break;
            }
        }

        for (SynthesisStartResultVo result : resultList) {
            SynthesisRecordConsumeResult record = new SynthesisRecordConsumeResult();
            record.setRecordId(synthesisRecord.getId());
            record.setRecordType(SynthesisRecordTypeEnum.RESULT);
            record.setName(result.getName());
            record.setImgUrl(result.getImgUrl());
            record.setType(result.getType());
            record.setThirdId(result.getThirdId());
            record.setNum(result.getProductNum());
            record.setOtherNo(result.getNftNumberValue());
            list.add(record);
        }

        return saveBatch(list);
    }
}
